// 商品管理脚本
$(function() {
	$("#searchid").linkbutton({
				text : '查询',
				iconCls : 'icon-search'
			});
	$('#messagegrid').datagrid({
		title : '您所在的位置：限时抢购管理模块&nbsp;&nbsp;--&gt;&gt;&nbsp;&nbsp;抢购产品设置',
		idField : 'id',
		width : 1000,
		height : 500,
		nowrap : false,
		striped : true,
		collapsible : false,
		url : '../goods/goods_search.shtml',
		frozenColumns : [[{
					field : 'ck',
					title : '选择',
					checkbox : true
				}]],
		columns : [[{
					title : '基本信息',
					colspan : 8
				}, {
					field : 'opt',
					title : '操作',
					width : 160,
					align : 'center',
					rowspan : 2,
					formatter : function(value, rec) {
					if (rec.id == undefined)
							return null;
						else
							return '<span style="color:red"><span onclick=onmodify("'
									+ rec.id
									+ '")><font style="cursor:pointer;color:blue;">修改</font></span> <span onclick=ondelete("'
									+ rec.id + '")><font style="cursor:pointer;color:blue;">删除</font></span></span><hr>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<select id="aud_'+rec.id +'"><option value="1">审核通过</option><option value="2">审核不通过</option></select>&nbsp;<span onclick=onAutding("'
									+ rec.id + '")><font style="cursor:pointer">审核</font></span></span>';
					}
				}], [{
					field : 'name',
					title : '商品名称',
					mapping : 'name',
					align : 'center',
					width : 120
				},{
					field : 'imgSrc',
					title : '商品图片',
					mapping : 'name',
					align : 'center',
					width : 165,
					formatter : function(value, rec) {
						if(value == ""){
							return '暂无图片';
						}
						return '<img src="../'+value+'" width="162px" height="113px"/>';
					}
				}, {
					field : 'price',
					title : '商品价格',
					align : 'center',
					mapping : 'price',
					width : 60,
					sortable : true
				}, {
					field : 'upprice',
					title : '抢购价格',
					width : 60,
					mapping : 'upprice',
					align : 'center' 
				},
				{
					field : 'createtimeString',
					title : '发布时间',
					width : 120,
					mapping : 'createtimeString',
					align : 'center' 
				},{
					field : 'overTimeString',
					title : '抢购结束时间',
					align : 'center',
					width : 120 
				},{
					field : 'activity',
					title : '是否有效',
					align : 'center',
					width : 120 
				},{
					field : 'auditingState',
					title : '核实状态',
					width : 60,
					formatter : function(value, rec) {
						if(value==1)return "通过";
					    if(value==2)return "未通过";
					    return  "未审核";
					},
					mapping : 'authorName',
					align : 'center' 
				}

		]],
		pagination : true,
		pageList:[10,20,50,80,100],
		rownumbers : true,
		singleSelect:true,
		queryParams : {
			name : name,
			categoryId1 :categoryId1,
   			categoryId2 :categoryId2,
			typeId :typeId,
			brandId :brandId
		},
		toolbar : [{
					id : 'btnadd',
					text : '发布信息',
					iconCls : 'icon-add',
					handler : function() {
						$(this).attr("href", '../goods/goods_addGoodsPage.shtml?menuCls=36');
					}
				}, '-', {
					id : 'btnsave',
					text : '删除信息',
					iconCls : 'icon-remove',
					handler : function() {
						var records = $('#messagegrid')
								.datagrid("getSelections");
						var ids = '';
						if (null == records || records == "") {
							alert('请选择要删除的记录');
							return;
						}
						for (var i = 0; i < records.length; i++) {
							if (records[i].id == undefined
									|| records[i].id == "undefined") {
								alert('此条记录已不存在');
								return;
							}
							ids += records[i].id;
							ids += '_';
						}
						if (confirm("确定删除所选信息吗?")) {
						var url1 = "../goods/goods_deleteGoods.shtml";
						$.ajax({
									url : url1,
									data : "idString=" + ids + "&type=1",
									success : function(msg) {
										var obj = eval('(' + msg + ')');
										var result = obj[0]["message"];
										alert(result);
										$('#messagegrid').datagrid("reload");
									}
								});
						}
					}
				},'-',{
					id : 'btnadd',
					text : '维护公告',
					iconCls : 'icon-add',
					handler : function() {
						var records = $('#messagegrid').datagrid("getSelections");
						var ids = '';
						if (null == records || records == "") {
							alert('请选择要维护的记录');
							return;
						}
						if(records.length>1){
							alert('只能逐条维护');
							return;
						}
						$(this).attr("href", '../goods/goods_findGood.shtml?id='+records[0].id+'&name='+$("#name").val()
												+'&bigCategory='+$("#bigCategory").val()+'&categoryId='+ $("#categoryId").val() 
												+'&typeId='+$("#typeId").val()+'&brandId='+ $("#brandId").val()
						);
					}
				},'-',{
					id : 'btnclose',
					text : '  &nbsp;关  闭',
					iconCls : 'icon-cancel',
					handler : function() {
						window.self.close();
					}
				}
				
				
				]
		}
	);
	$("body").keydown(function(){
	     if(event.keyCode==13)
     {
      onSearch();

      }
	});
});

function onSearch() {
	var queryParams = $('#messagegrid').datagrid("options").queryParams;
	queryParams.name = $("#name").val();
	if($("#brandId").val()==''||$("#brandId").val()==undefined){
    	if($("#typeId").val()==''||$("#typeId").val()==undefined){
    		if($("#categoryId").val()==''||$("#categoryId").val()==undefined){
    			queryParams.categoryId1 =$("#bigCategory").val();
    			queryParams.categoryId2 ='';
				queryParams.typeId ='';
				queryParams.brandId ='';
	    	}else{
	    		queryParams.categoryId1 ='';
	    		queryParams.categoryId2 =$("#categoryId").val();
				queryParams.typeId ='';
				queryParams.brandId ='';
	    	}
    	}else{
    		queryParams.categoryId1 ='';
    		queryParams.categoryId2 ='';
			queryParams.typeId =$("#typeId").val();
			queryParams.brandId ='';
    	}
    }else{
    	queryParams.categoryId1 ='';
    	queryParams.categoryId2 ='';
		queryParams.typeId ='';
		queryParams.brandId =$("#brandId").val();
    }
	$('#messagegrid').datagrid("reload");
}

function ondelete(id) {
	if (id == "undefined") {
		alert("此条记录已删除");
		return;
	}
	if (confirm("确定删除此条信息吗?")) {
		var url1 = "../goods/goods_deleteGoods.shtml";
		$.ajax({
				url : url1,
				data : "id=" + id,
				success : function(msg) {
					var obj = eval('(' + msg + ')');
					var result = obj[0]["message"];
					alert(result);
					$('#messagegrid').datagrid("reload");
				}
			});
	}
}
function onmodify(id) {
	if (id == "undefined") {
		alert("此条记录已被删除");
		return;
	}
	window.location.href = "../goods/goods_searchById.shtml?menuCls=36&id=" + id;
}
function onAutding(id){
		if (id == "undefined") {
		alert("此条记录已删除");
		return;
	}
	if (confirm("确定审核此条信息吗?")) {
        var autding=document.getElementById("aud_"+id).value;
		var url1 = "../goods/goods_updateMessageById.shtml";
			$.ajax({
					url : url1,
					data : "id=" + id+"&autding="+autding ,
					cache:false,
					success : function(msg) {
   					   var obj = eval('(' + msg + ')');
                       var result = obj["result"];
                         if(!result){
                                      alert("审核失败");
                                      return;
		                     }
		                  alert("审核成功");
						$('#messagegrid').datagrid("reload");
					}
				});
	}
}
 function bigCategoryChange(){
	var bigCategoryValue = $("#bigCategory option:selected").val();
	if(bigCategoryValue != ""){
		$('#smallCategory').html('加载中... ...');
		$.get('../category/category_loadCategoryByPid.shtml?id='+bigCategoryValue+'&t='+new Date().getTime().toString(36),function(mes){
			var json = eval('('+mes+')');
			var str = '<select name="tgAnnouncement.categoryId" id="categoryId" ><option value="">请选择</option>';
			for(var i = 0; i < json.length; i++){
				str += '<option value="'+json[i]['id']+'">'+json[i]['name']+'</option>';
			}
			str+='</select>';
			$('#smallCategory').html(str);
		})
	}
}
function bigCategoryChange(){
	var bigCategoryValue = $("#bigCategory option:selected").val();
	if(bigCategoryValue != ""){
		$('#smallCategory').html('加载中... ...');
		$('#goods').html('');
		$('#goodsType').html('');
		cacheData = '';
		$.get('../category/category_loadCategoryByPid.shtml?id='+bigCategoryValue+'&t='+new Date().getTime().toString(36),function(mes){
			var json = eval('('+mes+')');
			var str = '<select name="categoryId" id="categoryId" onchange="smallCategoryChange();" ><option value="">请选择</option>';
			for(var i = 0; i < json.length; i++){
				str += '<option value="'+json[i]['id']+'">'+json[i]['name']+'</option>';
			}
			str+='</select>';
			$('#smallCategory').html(str);
		})
	}
}

var cacheData;
function smallCategoryChange(){
	var smallCategoryValue = $("#categoryId option:selected").val();
	if(smallCategoryValue != ""){
		$('#goodsType').html('加载中... ...');
		$.get('../type/type_loadTypeByCategoryId.shtml?categoryId='+smallCategoryValue+'&t='+new Date().getTime().toString(36),function(mes){
			var json = eval('('+mes+')');
			var str = '<select name="typeId" id="typeId" onchange="threechange();" ><option value="">请选择</option>';
			for(var i = 0; i < json.length; i++){
				str += '<option value="'+json[i]['id']+'">'+json[i]['name']+'</option>';
			}
			str+='</select>';
			$('#goodsType').html(str);
		})
		$('#goods').html('加载中... ...');
		$.get('../brand/findAllBrandByCategory.shtml?categoryId='+smallCategoryValue+'&t='+new Date().getTime().toString(36),function(mes){
			var json = eval('('+mes+')');
			var str = '<select name="brandId" id="brandId"><option value="">请选择</option>';
			for(var i = 0; i < json.length; i++){
				str += '<option value="'+json[i]['id']+'">'+json[i]['name']+'</option>';
			}
			str+='</select>';
			cacheData = str;
			$('#goods').html(str);
		})
	}
}
function threechange(){
	var value2 = $("#typeId option:selected").val();
	if(value2==undefined)value2='';
	if(value2 != ""){
		$('#goods').html('加载中... ...');
		$.get('../brand/findAllBrandByType.shtml?typeId='+value2+'&t='+new Date().getTime().toString(36),function(mes){
			var json = eval('('+mes+')');
			var str = '<select name="brandId" id="brandId"><option value="">请选择</option>';
			for(var i = 0; i < json.length; i++){
				str += '<option value="'+json[i]['id']+'">'+json[i]['name']+'</option>';
			}
			str+='</select>';
			$('#goods').html(str);
		})
	}else{
		$('#goods').html(cacheData);
	}
}
